﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MPMusicVids.Data;
using SQLite.NET;
using System.Collections;
using System.Drawing.Drawing2D;
using MediaPortal.Configuration;

namespace MPMusicVids
{
    public partial class MainPanel : UserControl
    {
        DataManager dm = MusicVidsMain.dm;
        SQLiteResultSet rs;


        public MainPanel()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            mainConfig mc = (mainConfig)this.Parent.Parent.Parent;
            mc.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            doScan();
        }

        private void doScan()
        {
            Rescan rs = new Rescan();
            if (rs.ShowDialog() == DialogResult.OK)
                reLoad();
        }

        public void reLoad()
        {
            //Reload the main window to show contents of DB
            rs = dm.getAll();
            Stack<string> artistStack = new Stack<string>();
            listArtists.Items.Clear();
            foreach (SQLiteResultSet.Row row in rs.Rows)
            {
                Artist artist = new Artist(row.fields[0], row.fields[1], row.fields[2], row.fields[3]);
                if (!(artistStack.Contains(row.fields[0])))
                {
                    listArtists.Items.Add(artist);
                    artistStack.Push(row.fields[0]);
                }
            }
            try
            {
                listArtists.SelectedIndex = 0;
            }
            catch {
                TabControl tc1 = (TabControl)this.Parent.Parent;
                    tc1.SelectedIndex = 2;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.IO.Directory.Delete(Config.GetFile(Config.Dir.Thumbs, "Music Vids\\Thumbs\\"), true);
                System.IO.Directory.Delete(Config.GetFile(Config.Dir.Thumbs, "Music Vids\\Artists\\"), true);
            }
            catch { }
            dm.Execute("DELETE FROM Artists");
            dm.Execute("DELETE FROM Videos");
            doScan();
        }

        private void listArtists_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadArtist();
        }

        private void LoadArtist()
        {
            //Populate all that with the stuff
            rs = dm.getAll();
            Artist selectedArtist = (Artist)listArtists.SelectedItem;
            string artistID = selectedArtist.ID;
            bool done = false;
            dgvSongs.Rows.Clear();
            foreach (SQLiteResultSet.Row row in rs.Rows)
            {
                if (row.fields[0] == artistID)
                {
                    //Update artist name, pic and bio
                    if (!done)
                    {
                        try
                        {
                            Image aImg = Image.FromFile(row.fields[3]);
                            artistImg.Image = aImg;
                            SetImage(artistImg);
                        }
                        catch 
                        {
                            artistImg.Image = artistImg.InitialImage;
                        }
                        artistName.Text = row.fields[1];
                        artistBio.Text = row.fields[2];
                        done = true;
                    }
                    //Just add a new song
                    dgvSongs.Rows.Add(row.fields[7], row.fields[6], row.fields[4]);
                }
            }
        }

        //Generate new image dimensions
        public Size GenerateImageDimensions(int currW, int currH, int destW, int destH)
        {
            double multiplier = 0;           
            string layout;           
            if (currH > currW) layout = "portrait";
            else layout = "landscape";

            switch (layout.ToLower())
            {
                case "portrait":                   
                    if (destH > destW)
                        multiplier = (double)destW / (double)currW;                   
                    else                   
                        multiplier = (double)destH / (double)currH;                   
                    break;
                case "landscape":
                    if (destH > destW)
                        multiplier = (double)destW / (double)currW;                   
                    else
                        multiplier = (double)destH / (double)currH;
                    break;
            }
            return new Size((int)(currW * multiplier), (int)(currH * multiplier));
        }
       
        private void SetImage(PictureBox pb)
        {
            try
            {
                Image img = pb.Image;
                Size imgSize = GenerateImageDimensions(img.Width, img.Height, pb.Width, pb.Height);
                Bitmap finalImg = new Bitmap(img, imgSize.Width, imgSize.Height);
                Graphics gfx = Graphics.FromImage(img);
                gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                pb.Image = null;
                pb.SizeMode = PictureBoxSizeMode.CenterImage;
                pb.Image = finalImg;
            }
            catch (System.Exception e)
            {
               
            }
        }

        private void dgvSongs_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            string filename = (string)dgvSongs.Rows[row].Cells[2].Value;
            //nOW PASS THE ID TO A WINDOW ENABLE EDITING
            SongDetails sd = new SongDetails(filename);
            if (sd.ShowDialog() == DialogResult.OK)
                LoadArtist();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            NewArtist editor = new NewArtist((Artist)listArtists.SelectedItem);
            if (editor.ShowDialog() == DialogResult.OK)
                reLoad();
        }

        private void btnManual_Click(object sender, EventArgs e)
        {
            if (new Manual().ShowDialog() == DialogResult.OK)
                reLoad();
        }
    }
}
